import 'package:flutter/material.dart';
import 'package:inview_notifier_list/inview_notifier_list.dart';

///builds a [ListView] and notifies when the widgets are on screen within a provided area.
///
///The constructor takes an [IndexedWidgetBuilder] which builds the children on demand.
///It's just like the [ListView.builder].
class MyInViewNotifierList extends InViewNotifier {
  MyInViewNotifierList({
    Key? key,
    List<String> initialInViewIds = const [],
    int contextCacheCount = 10,
    double endNotificationOffset = 0.0,
    VoidCallback? onListEndReached,
    Duration throttleDuration = const Duration(milliseconds: 200),
    required Widget child,
    required IsInViewPortCondition isInViewPortCondition,
  })   : assert(contextCacheCount >= 1),
        assert(endNotificationOffset >= 0.0),
        super(
          key: key,
          initialInViewIds: initialInViewIds,
          endNotificationOffset: endNotificationOffset,
          onListEndReached: onListEndReached,
          throttleDuration: throttleDuration,
          contextCacheCount: contextCacheCount,
          isInViewPortCondition: isInViewPortCondition,
          child: child,
        );

  static InViewState? of(BuildContext context) {
    final InheritedInViewWidget widget = context
        .getElementForInheritedWidgetOfExactType<InheritedInViewWidget>()!
        .widget as InheritedInViewWidget;
    return widget.inViewState;
  }
}
